layui.extend({
    admin: '{/}../../js/admin'
});

layui.use(['table', 'jquery', 'form', 'admin'], function () {
    var table = layui.table,
        $ = layui.jquery,
        form = layui.form,
        admin = layui.admin;

    tableIns = table.render({
        elem: '#jobDetailList',
        cellMinWidth: 80,
        url: "../../quartz/detail/triggerList",
        method: 'POST',
        where: {},
        cols: [[
            {type: 'checkbox'},
            {field: 'id', title: 'id', sort: true},
            {field: 'jobName', title: '任务名称', sort: true},
            {field: 'jobGroup', title: '任务分组', sort: true},
            {field: 'jobKey', title: '任务Key', sort: true},
            {field: 'triggerType', title: '运行类型', sort: true},
            {field: 'triggerKey', title: '运行Key', sort: true},
            {
                field: 'triggerStatus', title: '运行状态', sort: true, templet: function (d) {
                    var text = "";
                    var runStatus = d.jobStatus;
                    if (runStatus == "NOT_RUN"){
                        text = "未运行";
                    }else if(runStatus == "WAIT_RUN"){
                        text = "等待运行";
                    }else if(runStatus == "RUN"){
                        text = "运行中";
                    }else {
                        text = "未知状态";
                    }
                    return text;
                }
            },
            {field: 'triggerRule', title: '运行规则', sort: true},
            {field: 'operate', title: '操作', toolbar: '#operateTpl', unresize: true}
        ]],
        event: true,
        page: true
    });
    table.on('tool(jobList)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除任务' + data.jobName + "吗？", function (index) {
                //发异步删除数据
                $.ajax({
                    url: "../quartz/removeJob",
                    data: {id: data.id},
                    dataType: "json",
                    success: function (data1) {
                        if (data1.code == 0) {
                            $(obj).parents("tr").remove();
                            obj.del();
                            layer.msg('已删除!', {
                                icon: 1,
                                time: 1000
                            });
                        } else {
                            layer.msg(data1.desc, {
                                icon: 1,
                                time: 1000
                            });
                        }
                    }
                });
                layer.close(index);

            });
        } else if (obj.event === 'edit') {
            WeAdminEdit('编辑', '../quartz/editJob.html?id=' + data.id, data.id, 600, 400)
        }
    });
    $(function () {
//监听提交
        form.on('submit(sreach)', function (data) {
            console.log(data.field);
            table.reload("jobList", {
                where: data.field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        });
    })


});